package com.wuliqing.android.zte.presenter;

import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;

import com.wuliqing.android.zte.data.ContactDetailData;
import com.wuliqing.android.zte.data.ContactQuery;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * Created by 10172915 on 2015/7/7.
 */
public class LoadContactDetailPresenter {
    private Activity mActivity;
    private LoadContactDataListener mLoadContactDataListener;
//    private List<CallLogData> mContactDataList;
    private ContactDetailData mContactData;
    private Uri mLookupUri;
    public LoadContactDetailPresenter(Activity activity, LoadContactDataListener listener, Uri uri) {
        mActivity = activity;
        mLoadContactDataListener = listener;
//        mContactDataList = list;
        mContactData = new ContactDetailData();
        mLookupUri = uri;
    }

    public interface LoadContactDataListener {
        void requestContactData(ContactDetailData contactData);
    }

    public void onStart() {
        mActivity.getLoaderManager().restartLoader(2, null, loaderCallbacks);
    }

    public void onDestroy() {
        mActivity.getLoaderManager().destroyLoader(2);
    }

    private final LoaderCallbacks<Cursor> loaderCallbacks = new LoaderCallbacks<Cursor>() {

        @Override
        public void onLoaderReset(Loader<Cursor> arg0) {

        }

        @Override
        public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
            cursor.moveToPosition(-1);
            while (cursor.moveToNext()) {
                if (cursor != null) {
                    setDataToView(cursor);
                }
            }

            if (null != mLoadContactDataListener) {
                mLoadContactDataListener.requestContactData(mContactData);
            }
        }

        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            if (mLookupUri != null) {
                Uri uri = Uri.withAppendedPath(mLookupUri,
                        ContactsContract.Contacts.Entity.CONTENT_DIRECTORY);
                return new CursorLoader(mActivity, uri,
                        ContactQuery.COLUMNS, null, null,
                        ContactsContract.Contacts.Entity.RAW_CONTACT_ID);
            } else {
                return null;
            }
        }
        public Bitmap getHighPhoto(String people_id, ContentResolver cr) {
            Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(people_id));
            InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri, true);
            if (input == null) {
                return null;
            }
            return BitmapFactory.decodeStream(input);
        }

        private void setDataToView(Cursor cursor) {
            String data_type = cursor.getString(ContactQuery.DATA2);
            String mime_type = cursor.getString(ContactQuery.MIMETYPE);
            int raw_contactId = cursor.getInt(ContactQuery.RAW_CONTACT_ID);
            mContactData.setContact_id(raw_contactId);
            //number
            if (data_type != null
                    && data_type.equals(String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE))
                    && ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mime_type)) {
                String number = cursor.getString(ContactQuery.DATA1);
                mContactData.setContact_number(number);
            }
            //email
            if (data_type != null
                    && data_type.equals(String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_HOME))
                    && ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mime_type)) {
                String email = cursor.getString(ContactQuery.DATA1);
                mContactData.setContact_email(email);
            }
            //address
            if (data_type != null
                    && data_type.equals(String.valueOf(ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME))
                    && ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mime_type)) {
                String address = cursor.getString(ContactQuery.DATA1);
                mContactData.setAddress(address);
            }
            //note
            if (ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mime_type)) {
                String organization = cursor.getString(ContactQuery.DATA1);
                mContactData.setOrganization(organization);
            }
            //note
            if (ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mime_type)) {
                String note = cursor.getString(ContactQuery.DATA1);
                mContactData.setNote(note);
            }
            String display_name = cursor.getString(ContactQuery.DISPLAY_NAME);
            mContactData.setContact_name(display_name);
            byte[] str = cursor.getBlob(ContactQuery.DATA15);
            mContactData.setPhoto(str);

            if (str != null) {
//                InputStream inputStream = new ByteArrayInputStream(str);
//                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                Bitmap bitmap = getHighPhoto(String.valueOf(raw_contactId),mActivity.getContentResolver());
                mContactData.setContact_photo_bitmap(bitmap);
            }
        }
    };

}
